package com.object.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * 图片的静态映射
 */

@Configuration
public class MyImage implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        determineWhetherTheFolderIsOperational();
        registry.addResourceHandler("/img/**").addResourceLocations("file:" + FileConfig.PATH);
        registry.addResourceHandler("/file/**").addResourceLocations("file:" + FileConfig.FILEPATH);
    }

    /**
     * 判断是否操作文件夹     部署服务器自动检测是否存在文件夹
     */
    private void determineWhetherTheFolderIsOperational() {
        File file = new File(FileConfig.rootPath + "/img");
        if (!file.exists()) {
            file.mkdirs();
        }
        File file1 = new File(FileConfig.rootPath + "/file");
        if (!file1.exists()) {
            file1.mkdirs();
        }
    }
}
